package com.wzgl.core.controller;

import java.math.BigInteger;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.wzgl.core.base.JBaseController;
import com.wzgl.core.interceptor.RequiredUserIdInterceptor;
import com.wzgl.core.model.Tdicinfo;
import com.wzgl.core.model.Tusers;
import com.wzgl.core.shop.ShopTseqCode;
import com.wzgl.core.utils.UuidUtil;
import com.wzgl.core.utils.WebUtils;

public class SeqCodeController extends JBaseController {
	@Before(RequiredUserIdInterceptor.class)
	public void seqCode_index() {
		List<Tdicinfo> tdic = Tdicinfo.dao.getTdicBySort("9000");
		setAttr("tdic", tdic);
		
		List<Tusers> users = Tusers.dao.getAll();
		setAttr("users", users);
		
		render("seqCode_list.jsp");
	}
	

	@Before(RequiredUserIdInterceptor.class)
	public void seqCode_edit(){
		String fid = getPara("fid");
		ShopTseqCode t =null;
		List<Tdicinfo> tdic = Tdicinfo.dao.getTdicBySort("9000");
		setAttr("tdic", tdic);
		if(StrKit.notBlank(fid)){
			t = ShopTseqCode.dao.findById(fid);
			setAttr("shoptseqcode", t);
		}
		render("seqCode_edit.jsp");
	}
	
	@Before(RequiredUserIdInterceptor.class)
	public void delSeq(){
		 String[] fid = getParaValues("id[]");
		 int count = 0;
		 for (String id : fid) {          
			 boolean b = ShopTseqCode.dao.deleteById(id);
			 if(b){
				 count++;
			 }
          }
		 renderJson(Ret.ok("msg",count));
	}
	
	
	@Before(RequiredUserIdInterceptor.class)
	public void seqCode_Save(){
		ShopTseqCode t = getModel(ShopTseqCode.class,"shoptseqcode");
		Ret r = new Ret();
		boolean b = false;
		if(StrKit.isBlank(t.getId())){
//			t.setFid(UuidUtil.get32UUID());
			if(t.getFcounts() != null && Integer.valueOf(t.getFcounts()) > 1){
				for(int i = 0;i<Integer.valueOf(t.getFcounts());i++){
					ShopTseqCode stc = new ShopTseqCode();
					stc.setFtype(t.getFtype());
					stc.setFsequence(UuidUtil.get32UUID().toUpperCase());
					stc.setFuserid(WebUtils.currentUser(this).getFnickname());
					stc.setFcycle(t.getFcycle());
					stc.setFremark(t.getFremark());
					b = stc.save();
				}
			}else{
				t.setFsequence(UuidUtil.get32UUID().toUpperCase());
				t.setFuserid(WebUtils.currentUser(this).getFnickname());
				b = t.save();
			}
		}else{
			b = t.update();
		}
		if(b){
			renderJson(r.ok());
		}else{
			renderJson(r.fail("msg", "保存失败,请稍后再试!"));
		}
	}
	/**
	 * 列表数据
	 */
	@Before(RequiredUserIdInterceptor.class)
	public void seqCode_list(){
		Page<ShopTseqCode> list_page;
		try {
			BigInteger page=getParaToBigInteger("page",BigInteger.valueOf(1));
			BigInteger pagesize=getParaToBigInteger("pagesize",BigInteger.valueOf(5));
			String fcode =  getParaTo("fcode");
			String userid =  getParaTo("userid");
			
			list_page= ShopTseqCode.dao.paginate(page.intValue(),pagesize.intValue(),fcode,userid);
			renderAjaxResult("",0,list_page);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			renderAjaxResultForError(e.getMessage());
		}
	}
}
